<?php
$_GET['delivery_state'] = isset($_GET['delivery_state']) && $_GET['delivery_state'] !== '' ? intval($_GET['delivery_state']) : '';
$orderby = get_major_key(array('id' => 'DESC'));
$oOrder = new Order();

if(!empty($_GET['btnExport'])){	// 导出
	
	$list = $oOrder->get_send_list('*', $_GET, $orderby, 1, 65536, $offset = 0);
	if(!empty($list['list']) && is_array($list['list'])){
		$data = array();
		foreach($list['list'] as $key => $row){
			$row['sn'] = $row['sn'].chr(32);
			$row['fspecs'] = specs_join($row['fspecs']);
			$row['consignee'] = $row['address']['consignee'];
			$row['fweight'] = format_weight_kg($row['goodsinfo']['weight'], $row['goodsinfo']['weightunit']);
			$row['addtime'] = ftime($row['addtime']);
			$data[] = $row;
		}
		$headers = array(
			'sn'				=> array('title' => '订单号',		'width' => '18'),
			'fspecs'			=> array('title' => '规格',		'width' => '25',		'wrap' => true),
			'username'			=> array('title' => '用户名',		'width' => '12.5'),
			'consignee'			=> array('title' => '收货人',		'width' => '12.5'),
			'goodsname'			=> array('title' => '商品名称',	'width' => '25',	 	'wrap' => true),
			'fweight'			=> array('title' => '重量（Kg）',	'width' => '12.5'),
			'number'			=> array('title' => '数量',		'width' => '8'),
			'cost'				=> array('title' => '金额（￥）',	'width' => '12.5'),
			'addtime'			=> array('title' => '下单时间', 	'width' => '18'),
		);
		if($list['total'] > 5000){	// 导出超过5000时用CSV
			export_csv($data, $headers, '_E_待发货商品_'.date('Ymd_His').'.csv');
		}else{
			$oExcel = new Excel();
			$oExcel->export($headers, $data, '_E_待发货商品_'.date('Ymd_His').'.xlsx');
		}
	}
	exit;
}

$list = $oOrder->get_send_list('*', $_GET, $orderby, $__page, $__pagesize);
if(!empty($list['list']) && is_array($list['list'])){
	
	// 主键排序字段
	make_major_key_options(array(
		'id'			=> '下单时间',
		'cost'			=> '订单总价',
	));
	
	foreach($list['list'] as $key => $row){
		if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
			if(!empty($row['goodsinfo']['filename'])){
				$row['url'] = PUB_DIR.'goods/'.$row['goodsinfo']['filename'].'.html';
			}else{
				$row['url'] = PUB_DIR.'goods/'.time_to_path($row['goodsinfo']['addtime']).'/'.$row['goodsinfo']['id'].'.html';
			}
		}else{
			$row['url'] = WEB_DIR."index.php?mdl=goods&do=view&id={$row['goodsinfo']['id']}";
		}
		$row['goodsinfo']['fweight'] = format_weight_out($row['goodsinfo']['weight'], $row['goodsinfo']['weightunit']);
		$row['addtime'] = ftime($row['addtime']);
		$list['list'][$key] = $row;
	}
	$vars['list'] = $list['list'];
	$vars['pagelist'] = $list['pagelist'];
}
?>